package com.dlc.beijingrfid.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import butterknife.BindView;
import butterknife.OnClick;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.commonlibrary.ble.BleWorker;
import com.dlc.commonlibrary.dialog.DoResultDialog;
import com.dlc.commonlibrary.widget.TitleBar;
import java.util.ArrayList;

/**
 * 标签管理
 */
public class TagManageActivity extends BaseActivity {

    public static final String EXTRA_RFIDS = "rfids";

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.item_unbind) FrameLayout mItemUnbind;
    @BindView(R.id.item_bind) FrameLayout mItemBind;
    @BindView(R.id.item_scrap) FrameLayout mItemScrap;
    private Handler mHandler;
    private DoResultDialog doReadDialog,doResultDialog;
    private StringBuilder mStringBuilder;
    private  Class<? extends Activity> mClass;
    BleWorker mBleWorker = new BleWorker();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleBar.leftExit(this);

        mHandler = new Handler();
        doReadRFID();
        doResult();
        mBleWorker.setCallback(new BleWorker.ReadCallback() {
            @Override
            public void onReceiveData(ArrayList<String> rfList,int readNum) {
                doReadDialog.dismiss();
                doResultDialog.showResultAndDismiss(true, 1000);
                if (rfList.size() > 0) {
                    doResultDialog.dismiss();
                    Intent intent = new Intent(getActivity(), mClass);
                    intent.putExtra(EXTRA_RFIDS, rfList);
                    startActivity(intent);
                } else {
                    doResultDialog.setFailedText("讀取不到RFID");
                    doResultDialog.setFailedImage(R.mipmap.ic_error);
                    doResultDialog.showResultAndDismiss(false, 2000);
                }
            }

            @Override
            public void onDeviceDisconnected() {
                doReadDialog.dismiss();
                Intent intent = new Intent(TagManageActivity.this,MainActivity.class);
                intent.putExtra("type","2");
                startActivity(intent);
                finish();
            }
            @Override
            public void onErrorNumber(int number) {
                if (number > 0) {
                    showOneToast("存在长度小于14的标签" + number + "个");
                }
            }
        });
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_tag_manage;
    }

    @OnClick({ R.id.item_unbind, R.id.item_bind, R.id.item_scrap })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.item_unbind:
                mClass = UnbindTagActivity.class;
                getData();

                break;
            case R.id.item_bind:
                mClass = BindTagActivity.class;
                getData();

                break;
            case R.id.item_scrap:
                mClass = BucaoScrapActivity.class;
                getData();

                break;
        }
    }
    private void test(){
        ArrayList<String> rfList = new ArrayList<>();
        String strings[] ={"0001D4CF76AE","0001ED52F9EC",
        };
        for (int i = 0;i < strings.length;i++){
            rfList.add(strings[i]);
        }
        Intent intent = new Intent(getActivity(), mClass);
        intent.putExtra(EXTRA_RFIDS, rfList);
        startActivity(intent);
    }

    private void getData(){
        doReadDialog.showWorking(true);
        mBleWorker.check();
        //test();
    }

    //private void doData(String data){
    //    rfidsList.add(data);
    //    LogPlus.e("蓝牙数据=" + data+"==蓝牙数据 count=" + rfidsList.size() + "");
    //}
    //
    //
    //private void judgeData(final Class<? extends Activity> cls) {
    //    runOnUiThread(new Runnable() {
    //        @Override
    //        public void run() {
    //            if (!isBleStop) {
    //                if (rfidsList.size() > 0) {
    //                    if (rfidsList.size() == cashCount || TYPE == 2) {//获取到的缓存区数量相等或者是直接获取缓存数据
    //                        ArrayList<String> mList = new ArrayList<>();
    //                        doReadDialog.dismiss();
    //                        doResultDialog.showResultAndDismiss(true, 1000);
    //                        for (String str : rfidsList) {
    //                            if (str.length() > 14) {
    //                                str = str.substring(str.length() - 12, str.length());
    //                                mList.add(str);
    //                            }
    //                        }
    //                        Intent intent = new Intent(getActivity(), cls);
    //                        intent.putExtra(EXTRA_RFIDS, mList);
    //                        startActivity(intent);
    //                    } else {
    //                        //ToastUtil.show(getContext(),"获取的数量不对应，直接获取读到的缓存rfid");
    //                        TYPE = 2;
    //                        rfidsList.clear();
    //                        RRUA100API.getInstance(new Handler()).A100_getSyncTagList();
    //                    }
    //                } else {
    //                    doResultDialog.setFailedText("读取不到RFID");
    //                    doResultDialog.setFailedImage(R.mipmap.ic_error);
    //                    doResultDialog.showResultAndDismiss(false, 2000);
    //                }
    //            }else {
    //                doReadDialog.dismiss();
    //                showToast("未连接设备，跳到连接页面");
    //                Intent intent = new Intent(getActivity(),MainActivity.class);
    //                intent.putExtra("type", "2");
    //                startActivity(intent);
    //            }
    //        }
    //    });
    //
    //}

    private void doReadRFID() {
        doReadDialog =
                DoResultDialog.newDialog(this).
                        setWorkingText(getString(R.string.read_rfid_ing))
                        .setSuccessImage(R.mipmap.ic_success)
                        .setSuccessText(getString(R.string.scan_over))
                        .setButton("完成扫描", new DoResultDialog.OnClickButtonListener() {
                            @Override
                            public void onClickButton(DoResultDialog dialog) {
                                mBleWorker.cancelCheck();
                            }
                        });

        doReadDialog.setCancelable(false);

    }
    private void doResult() {

        doResultDialog = DoResultDialog.newDialog(this)
                .setWorkingText(getString(R.string.commit_counting_result))
                .setSuccessImage(R.mipmap.ic_success)
                .setSuccessText(getString(R.string.commit_counting_result_success));

        doResultDialog.setCancelable(false);

    }


}
